﻿<Window x:Class="Fluffy.MainWindowView"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
        xmlns:behaviors="clr-namespace:Fluffy.Library.Behaviors;assembly=Fluffy.Library"
        xmlns:fluffy="clr-namespace:Fluffy"
        xmlns:fileMenuCommands="clr-namespace:Fluffy.FileMenuCommands"
        xmlns:fakeDocument="clr-namespace:Fluffy.FakeDocument;assembly=Fluffy.FakeDocument"
        xmlns:archive="clr-namespace:Fluffy.Archive;assembly=Fluffy.Archive"
        xmlns:appMenu="clr-namespace:Fluffy.AppMenu"
        xmlns:torrent="clr-namespace:Fluffy.Torrent;assembly=Fluffy.Torrent"
        xmlns:text="clr-namespace:Fluffy.Text;assembly=Fluffy.Text"
        x:ClassModifier="internal"
        MaxHeight="{DynamicResource {x:Static SystemParameters.MaximizedPrimaryScreenHeightKey}}"
        MaxWidth="{DynamicResource {x:Static SystemParameters.MaximizedPrimaryScreenWidthKey}}" 
        Title="{Binding ApplicationTitle}" Height="480.208" Width="1030.208" WindowStyle="None" AllowsTransparency="True" 
        ResizeMode="CanResizeWithGrip" BorderThickness="6,6,4,8" Background="Transparent" MinWidth="800" MinHeight="500">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="pack://application:,,,/Fluffy.Library;component/Resources/Skin.xaml"/>
            </ResourceDictionary.MergedDictionaries>

            <BooleanToVisibilityConverter x:Key="booleanToVisibilityConverter"/>
            
        </ResourceDictionary>
    </Window.Resources>

    <Window.DataContext>
        <fluffy:MainWindowViewModel/>
    </Window.DataContext>

    <Window.TaskbarItemInfo>
        <TaskbarItemInfo ProgressState="{Binding CurrentFileContainer.ProgressState, Mode=OneWay, FallbackValue={x:Static TaskbarItemProgressState.None}}" ProgressValue="{Binding CurrentFileContainer.ProgressValue, Mode=OneWay, FallbackValue=0}"/>
    </Window.TaskbarItemInfo>

    <Window.InputBindings>
        <KeyBinding Gesture="Ctrl+O" Command="{Binding OpenCommand}" />
        <KeyBinding Gesture="Ctrl+N" Command="{Binding NewCommand}" />
        <KeyBinding Gesture="Ctrl+S" Command="{Binding CurrentFileContainer.SaveCommand}" />
    </Window.InputBindings>

    <i:Interaction.Behaviors>
        <behaviors:RememberWindowPlacementBehavior/>
        <behaviors:CloseWindowBehavior Command="{Binding WindowClosedCommand}"/>
        <behaviors:LoadedWindowBehavior Command="{Binding WindowLoadedCommand}"/>
    </i:Interaction.Behaviors>

    <Border BorderThickness="1" BorderBrush="{DynamicResource AppMainColor}">
        <Grid Background="{x:Static SystemColors.WindowBrush}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="20"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            
            <!-- Header -->
            <Grid Grid.Row="0" Grid.ColumnSpan="2" Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}">
                <i:Interaction.Behaviors>
                    <behaviors:DragWindowPanelBehavior/>
                </i:Interaction.Behaviors>
                <Grid.Resources>
                    <Style x:Key="captionButtonStyle" TargetType="Button">
                        <Setter Property="FontFamily" Value="Webdings"/>
                        <Setter Property="Width" Value="{DynamicResource {x:Static SystemParameters.WindowCaptionButtonWidthKey}}"/>
                        <Setter Property="Height" Value="{DynamicResource {x:Static SystemParameters.WindowCaptionButtonHeightKey}}"/>
                        <Setter Property="BorderBrush" Value="Transparent"/>
                        <Setter Property="BorderThickness" Value="0"/>
                        <Setter Property="Background" Value="Transparent"/>
                    </Style>
                </Grid.Resources>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="Auto"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0" Visibility="{Binding IsContentView, Converter={StaticResource booleanToVisibilityConverter}}">
                    <StackPanel Orientation="Horizontal">
                        <Button Content="File" Background="{DynamicResource AppMainColor}" Foreground="{DynamicResource AppMainForegraundColor}" Command="{Binding FileViewCommand}" Width="60"/>
                        <Menu Background="Transparent">
                            <Menu.Resources>
                                <appMenu:MenuStyleSelector x:Key="MenuStyleSelector">
                                    <appMenu:MenuStyleSelector.SeparatorStyle>
                                        <Style TargetType="MenuItem">
                                            <Setter Property="Template">
                                                <Setter.Value>
                                                    <ControlTemplate>
                                                        <Separator  Style="{StaticResource {x:Static MenuItem.SeparatorStyleKey}}"/>
                                                    </ControlTemplate>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </appMenu:MenuStyleSelector.SeparatorStyle>
                                    <appMenu:MenuStyleSelector.CommandStyle>
                                        <Style TargetType="MenuItem">
                                            <Setter Property="Header" Value="{Binding Header}"/>
                                            <Setter Property="Command" Value="{Binding Command}"/>
                                        </Style>
                                    </appMenu:MenuStyleSelector.CommandStyle>
                                </appMenu:MenuStyleSelector>
                            </Menu.Resources>
                            <MenuItem Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}">
                                <MenuItem.Header>
                                    <TextBlock Text="Edit" TextAlignment="Center" Margin="5,0"/>
                                </MenuItem.Header>
                                <MenuItem.Items>
                                    <MenuItem Header="Cut" Command="{Binding CurrentFileContainer.CutCommand}"/>
                                    <MenuItem Header="Copy" Command="{Binding CurrentFileContainer.CopyCommand}"/>
                                    <MenuItem Header="Paste" Command="{Binding CurrentFileContainer.PasteCommand}"/>
                                </MenuItem.Items>
                            </MenuItem>
                            <MenuItem Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}">
                                <MenuItem.Header>
                                    <TextBlock Text="View" TextAlignment="Center" Margin="5,0"/>
                                </MenuItem.Header>
                                <MenuItem.Items>
                                    <MenuItem Header="Archive" IsCheckable="True" IsChecked="{Binding IsOpenedAsArchive}"/>
                                    <MenuItem Header="Torrent" IsCheckable="True" IsChecked="{Binding IsOpenedAsTorrent}"/>
                                    <MenuItem Header="Text" IsCheckable="True" IsChecked="{Binding IsOpenedAsText}"/>
                                </MenuItem.Items>
                            </MenuItem>
                            <MenuItem Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}"
                                      ItemsSource="{Binding CurrentFileContainer.Menu1.Children}" ItemContainerStyleSelector="{StaticResource MenuStyleSelector}">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding CurrentFileContainer.Menu1.Header}" TextAlignment="Center" Margin="5,0"/>
                                </MenuItem.Header>
                                <MenuItem.Style>
                                    <Style TargetType="MenuItem">
                                        <Setter Property="Visibility" Value="{Binding CurrentFileContainer.Menu1.IsVisible, Converter={StaticResource booleanToVisibilityConverter}}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding CurrentFileContainer}" Value="{x:Null}">
                                                <Setter Property="Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </MenuItem.Style>
                            </MenuItem>
                            <MenuItem Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}"
                                      ItemsSource="{Binding CurrentFileContainer.Menu2.Children}" ItemContainerStyleSelector="{StaticResource MenuStyleSelector}">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding CurrentFileContainer.Menu2.Header}" TextAlignment="Center" Margin="5,0"/>
                                </MenuItem.Header>
                                <MenuItem.Style>
                                    <Style TargetType="MenuItem">
                                        <Setter Property="Visibility" Value="{Binding CurrentFileContainer.Menu2.IsVisible, Converter={StaticResource booleanToVisibilityConverter}}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding CurrentFileContainer}" Value="{x:Null}">
                                                <Setter Property="Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </MenuItem.Style>
                            </MenuItem>
                            <MenuItem Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}"
                                      ItemsSource="{Binding CurrentFileContainer.Menu3.Children}" ItemContainerStyleSelector="{StaticResource MenuStyleSelector}">
                                <MenuItem.Header>
                                    <TextBlock Text="{Binding CurrentFileContainer.Menu3.Header}" TextAlignment="Center" Margin="5,0"/>
                                </MenuItem.Header>
                                <MenuItem.Style>
                                    <Style TargetType="MenuItem">
                                        <Setter Property="Visibility" Value="{Binding CurrentFileContainer.Menu3.IsVisible, Converter={StaticResource booleanToVisibilityConverter}}"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding CurrentFileContainer}" Value="{x:Null}">
                                                <Setter Property="Visibility" Value="Collapsed"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </MenuItem.Style>
                            </MenuItem>
                            <MenuItem Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}">
                                <MenuItem.Header>
                                    <TextBlock Text="Tools" TextAlignment="Center" Margin="5,0"/>
                                </MenuItem.Header>
                                <MenuItem.Items>
                                    <MenuItem Header="Options" Command="{Binding OptionsCommand}"/>
                                </MenuItem.Items>
                            </MenuItem>
                            <MenuItem Height="{DynamicResource {x:Static SystemParameters.WindowCaptionHeightKey}}">
                                <MenuItem.Header>
                                    <TextBlock Text="Help" TextAlignment="Center" Margin="5,0"/>
                                </MenuItem.Header>
                                <MenuItem.Items>
                                    <MenuItem Header="Update" Command="{Binding UpdateCommand}"/>
                                    <Separator/>
                                    <MenuItem Header="Documentation" Command="{Binding DocumentationCommand}"/>
                                    <MenuItem Header="Website" Command="{Binding WebsiteCommand}"/>
                                    <Separator/>
                                    <MenuItem Header="About" Command="{Binding AboutCommand}"/>
                                </MenuItem.Items>
                            </MenuItem>
                        </Menu>
                    </StackPanel>
                </Grid>
                <TextBlock Grid.Column="1" Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=Title}" TextAlignment="Center" VerticalAlignment="Center"/>
                <Button Grid.Column="2" Content="0" Style="{StaticResource captionButtonStyle}">
                    <i:Interaction.Behaviors>
                        <behaviors:MinimizeButtonBehavior/>
                    </i:Interaction.Behaviors>
                </Button>
                <Button Grid.Column="3">
                    <Button.Style>
                        <Style TargetType="Button" BasedOn="{StaticResource captionButtonStyle}">
                            <Setter Property="Content" Value="1"/>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}, Path=WindowState}" Value="{x:Static WindowState.Maximized}">
                                    <Setter Property="Content" Value="2"/>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Button.Style>
                    <i:Interaction.Behaviors>
                        <behaviors:MaximizeButtonBehavior/>
                    </i:Interaction.Behaviors>
                </Button>
                <Button Grid.Column="4" Content="r" Style="{StaticResource captionButtonStyle}">
                    <i:Interaction.Behaviors>
                        <behaviors:CloseButtonBehavior/>
                    </i:Interaction.Behaviors>
                </Button>
            </Grid>

            <!-- Content -->
            <ContentControl Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Content="{Binding CurrentFileContainer}" Visibility="{Binding IsContentView, Converter={StaticResource booleanToVisibilityConverter}}">
                <ContentControl.Resources>
                    <DataTemplate DataType="{x:Type archive:ArchiveDocumentViewModel}">
                        <archive:ArchiveDocumentView/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type torrent:TorrentDocumentViewModel}">
                        <torrent:TorrentDocumentView/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type text:TextDocumentViewModel}">
                        <text:TextDocumentView/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type fakeDocument:FakeDocumentViewModel}">
                        <fakeDocument:FakeDocumentView/>
                    </DataTemplate>
                </ContentControl.Resources>
            </ContentControl>

            <!-- Status bar -->
            <Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" Background="{DynamicResource AppMainColor}" TextBlock.Foreground="{DynamicResource AppMainForegraundColor}"
                  Visibility="{Binding IsContentView, Converter={StaticResource booleanToVisibilityConverter}}">
                <StackPanel Orientation="Horizontal" VerticalAlignment="Center" Margin="10,0">
                    <TextBlock Text="Ready"/>
                </StackPanel>    
            </Grid>
            
            <!-- File menu content -->
            <Border Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" Background="{DynamicResource AppMainColor}" Visibility="{Binding IsFileMenuView, Converter={StaticResource booleanToVisibilityConverter}}">
                <StackPanel Orientation="Vertical">
                    <StackPanel.Resources>
                        <Style TargetType="Button">
                            <Setter Property="MinHeight" Value="25"/>
                            <Setter Property="Background" Value="Transparent"/>
                            <Setter Property="Foreground" Value="{DynamicResource AppMainForegraundColor}"/>
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="Button">
                                        <Border MinWidth="{TemplateBinding MinWidth}" Background="{TemplateBinding Background}" MinHeight="{TemplateBinding MinHeight}">
                                            <Label Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="Left" Margin="20,0"/>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <Trigger Property="IsMouseOver" Value="True">
                                                <Setter Property="Background" Value="#22000000"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </StackPanel.Resources>
                    <Button Command="{Binding ContentViewCommand}" Margin="20,10">
                        <Button.Template>
                            <ControlTemplate>
                                <Border Background="Transparent" HorizontalAlignment="Left">
                                    <Path Fill="{TemplateBinding Foreground}" Data="M37.324,10.003c-5.523-9.565-17.755-12.843-27.32-7.32C0.438,8.205-2.84,20.438,2.684,30.004
			                                                c5.522,9.565,17.754,12.843,27.32,7.32C39.569,31.801,42.847,19.569,37.324,10.003z M28.004,33.859
			                                                c-7.653,4.418-17.438,1.797-21.856-5.856c-4.419-7.652-1.797-17.438,5.856-21.856c7.652-4.418,17.438-1.796,21.856,5.857
			                                                C38.278,19.656,35.656,29.441,28.004,33.859z M27.004,18.003h-8.343l2.242-2.242c0.78-0.781,0.78-2.048,0-2.828
			                                                c-0.781-0.781-2.048-0.781-2.829,0l-5.656,5.656c-0.781,0.781-0.781,2.048,0,2.829l5.656,5.656c0.781,0.781,2.048,0.781,2.829,0
			                                                c0.78-0.781,0.78-2.047,0-2.828l-2.243-2.242h8.344c1.104,0,2-0.896,2-2v-0.001C29.004,18.898,28.108,18.003,27.004,18.003z"/>
                                </Border>
                            </ControlTemplate>
                        </Button.Template>
                    </Button>
                    <Button Command="{Binding NewCommand}" Content="New"/>
                    <Button Command="{Binding OpenCommand}" Content="Open"/>
                    <Button Command="{Binding CurrentFileContainer.SaveCommand}" Content="Save"/>
                    <Button Command="{Binding CurrentFileContainer.SaveAsCommand}" Content="Save As"/>
                    <Button Command="{Binding PrintCommand}" Content="Print"/>
                    <Button Command="{Binding ShareCommand}" Content="Share"/>
                    <Button Command="{Binding ExportCommand}" Content="Export"/>
                    <Button Command="{Binding CloseCommand}" Content="Close"/>
                    <Separator Margin="20,5"/>
                    <Button Command="{Binding OptionsCommand}" Content="Options"/>
                </StackPanel>
            </Border>
            
            <ContentControl Grid.Row="1" Grid.Column="1" Content="{Binding CurrentFileMenuOperation}" Visibility="{Binding IsFileMenuView, Converter={StaticResource booleanToVisibilityConverter}}" Grid.RowSpan="2">
                <ContentControl.Resources>
                    <DataTemplate DataType="{x:Type fileMenuCommands:OpenCommandViewModel}">
                        <fileMenuCommands:OpenCommandView/>
                    </DataTemplate>
                    <DataTemplate DataType="{x:Type fileMenuCommands:NewCommandViewModel}">
                        <fileMenuCommands:NewCommandView/>
                    </DataTemplate>
                </ContentControl.Resources>
            </ContentControl>

            <!-- Recent documents -->
            <Grid Grid.Row="0" Grid.Column="0" Grid.RowSpan="3" Background="{DynamicResource AppMainColor}" TextBlock.Foreground="{DynamicResource AppMainForegraundColor}"  Visibility="{Binding IsStartView, Converter={StaticResource booleanToVisibilityConverter}}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" Text="Recent Documents" HorizontalAlignment="Stretch" TextAlignment="Center" FontSize="20"/>
                <fileMenuCommands:RecentDocumentsView Grid.Row="1" DataContext="{Binding OpenCommandViewModel}" Foreground="{DynamicResource AppMainForegraundColor}"/>
                <Separator Grid.Row="2"/>
                <Button Grid.Row="3" Command="{Binding BrowseForOpenDocumentCommand}" Content="Open" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="20,0" Cursor="{x:Static Cursors.Hand}"
                        Foreground="{DynamicResource AppMainForegraundColor}">
                    <Button.Template>
                        <ControlTemplate>
                            <Border BorderThickness="0" Background="Transparent">
                                <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Center">
                                    <Path Fill="{TemplateBinding Foreground}" Data="M 33.185 7.58 h -6.533 V 6.571 h 6.533 V 7.58 Z M 33.185 8.238 h -6.533 v 1.011 h 6.533 V 8.238 Z M 33.181 3.661 h -6.523 v 2.223 h 6.523 V 3.661 Z M 35.268 1.271 v 23.827 c -0.001 0.357 -0.292 0.648 -0.65 0.648 h -9.397 c -0.359 0 -0.649 -0.291 -0.649 -0.649 V 1.271 c 0 -0.358 0.29 -0.65 0.649 -0.65 h 9.397 C 34.977 0.621 35.268 0.913 35.268 1.271 Z M 33.969 1.918 h -8.1 v 22.53 h 8.1 V 1.918 Z M 33.185 9.907 h -6.533 v 1.008 h 6.533 V 9.907 Z M 32.122 17.424 c 0 1.162 -0.939 2.099 -2.097 2.099 c -1.16 0 -2.099 -0.938 -2.099 -2.099 c 0 -1.158 0.94 -2.098 2.099 -2.098 C 31.183 15.326 32.122 16.267 32.122 17.424 Z M 31.365 17.424 c 0 -0.739 -0.6 -1.341 -1.341 -1.341 c -0.742 0 -1.342 0.601 -1.342 1.341 s 0.6 1.34 1.342 1.34 C 30.765 18.764 31.365 18.164 31.365 17.424 Z M 22.787 5.192 v 13.505 c 0 0.814 -0.655 1.473 -1.461 1.473 H 14.03 c 0 0 -0.414 2.948 2.212 2.948 v 1.475 H 14.03 H 9.608 H 7.396 V 23.12 c 2.529 0 2.212 -2.948 2.212 -2.948 H 2.315 c -0.809 0 -1.465 -0.659 -1.465 -1.473 V 5.192 c 0 -0.814 0.656 -1.47 1.465 -1.47 h 19.01 C 22.132 3.722 22.787 4.378 22.787 5.192 Z M 13.182 18.368 c 0 -0.698 -0.566 -1.263 -1.264 -1.263 c -0.699 0 -1.266 0.565 -1.266 1.263 c 0 0.698 0.566 1.265 1.266 1.265 C 12.617 19.633 13.182 19.066 13.182 18.368 Z M 21.221 5.285 H 2.418 v 11.387 H 21.22 h 0.001 V 5.285 Z M 11.931 17.577 c -0.434 0 -0.785 0.352 -0.785 0.785 s 0.352 0.785 0.785 0.785 s 0.785 -0.352 0.785 -0.785 S 12.365 17.577 11.931 17.577 Z"/>
                                    <TextBlock Text="Open other documents" VerticalAlignment="Center" Margin="10,0,0,0" Foreground="{TemplateBinding Foreground}"/>
                                </StackPanel>
                            </Border>
                        </ControlTemplate>
                    </Button.Template>
                </Button>
            </Grid>
            <Grid Grid.Row="1" Grid.Column="1" Grid.RowSpan="2" Visibility="{Binding IsStartView, Converter={StaticResource booleanToVisibilityConverter}}">
                <fileMenuCommands:NewCommandView DataContext="{Binding NewCommandViewModel}"/>
            </Grid>

        </Grid>
    </Border>
</Window>
